<?php

/**
 * Initialize the system
 */
if(defined('TL_ROOT'))
	require_once(TL_ROOT.'/system/initialize.php');
else
	require_once('system/initialize.php');

class Currency extends Controller
{
	/**
	 * This is class construct function.
	 * @params null
	 * @Return null
	 */
	public function __construct()
	{
		parent::__construct();		
	}
	
	/**
	 * This is function to change the order currency
	 * @param string price
	 * @param string define currency
	 * @return string currency price
	 */
	public function change_order($price='',$def_currency='')
	{
		if (!$price) $price = 0;
		
		$this->import("Database");
		$obj_currency = $this->Database->prepare("SELECT * FROM `tl_currency` WHERE `is_default`=1")
										->execute(time());
		
		if (!$_COOKIE['DAFT_CURRENCY'])
			$_COOKIE['DAFT_CURRENCY'] = 'RMB';
		if (!$def_currency)
			$def_currency = $_COOKIE['DAFT_CURRENCY'];
			
		$obj_select_currency = $this->Database->prepare("SELECT * FROM `tl_currency` WHERE `iso_code`=?")
										->execute($def_currency);
			
		return $obj_select_currency->sign . number_format($price*$obj_select_currency->conversion_rate,2,'.','');
	}
	
	/**
	 * This function is display was iso_code currency
	 * @param string price
	 * @param string define currency
	 * @return string currency price
	 */
	public function change_iso_order($price='',$def_currency='')
	{
		if (!$price) $price = 0;
		
		$this->import("Database");
		$obj_currency = $this->Database->prepare("SELECT * FROM `tl_currency` WHERE `is_default`=1")
										->execute(time());
		
		if (!$_COOKIE['DAFT_CURRENCY'])
			$_COOKIE['DAFT_CURRENCY'] = 'RMB';
		if (!$def_currency)
			$def_currency = $_COOKIE['DAFT_CURRENCY'];
		$obj_select_currency = $this->Database->prepare("SELECT * FROM `tl_currency` WHERE `iso_code`=?")
										->execute($def_currency);
			
		return 'WAS '.$obj_select_currency->sign . '<em>' . number_format($price*$obj_select_currency->conversion_rate,2,'.','') . '</em>';
	}
	
	public function get_currency_value($price, $def_currency='')
	{
		if (!$price) $price = 0;
		
		$this->import("Database");
		$obj_currency = $this->Database->prepare("SELECT * FROM `tl_currency` WHERE `is_default`=1")
										->execute(time());
		
		if (!$_COOKIE['DAFT_CURRENCY'])
			$_COOKIE['DAFT_CURRENCY'] = 'RMB';
		if (!$def_currency)
			$def_currency = $_COOKIE['DAFT_CURRENCY'];
		$obj_select_currency = $this->Database->prepare("SELECT * FROM `tl_currency` WHERE `iso_code`=?")
										->execute($def_currency);
		return $price * $obj_select_currency->conversion_rate;
	}
}